package com.baseframe.score.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baseframe.mybatis.utils.PageInfo;
import com.baseframe.score.entity.request.SnsScoreUserRequest;
import com.baseframe.score.entity.vo.SnsScoreUserVO;
import com.baseframe.score.service.SnsScoreUserService;
import com.baseframe.score.sign.ScoreChangeType;
import com.baseframe.tool.container.Kv;
import com.baseframe.tool.result.R;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 管理员用户积分规则控制器
 */
@RestController
@RequestMapping("/back/score/user")
@AllArgsConstructor
public class BackSnsScoreUserController {

    private SnsScoreUserService snsScoreUserService;


    /**
     * 采用枚举类型返回积分变更类型
     * @return
     */
    @GetMapping("/change-type/list")
    public R<List<Kv>> changeTypeType(){
        // 获取所有的枚举类型
        ScoreChangeType[] values = ScoreChangeType.values();
        List<Kv> changeTypes = new ArrayList<>();
        // 遍历枚举类型, 封装成kv对象
        for (ScoreChangeType changeType : values) {
            Kv kv = Kv.create();
            kv.put("type",changeType.getCode());
            kv.put("name",changeType.getName());
            changeTypes.add(kv);
        }
        // 返回
        return R.data(changeTypes);
    }

    /**
     * 变更积分
     * @param request
     * @return
     */
    @PostMapping("/change")
    public R<Object> changeScore(@RequestBody SnsScoreUserRequest request){
        if (request.getId()==null||request.getChangeValue()==null||request.getChangeType()==null){
            return R.fail("缺少必要请求参数~");
        }
        if (ScoreChangeType.getChangeType(request.getChangeType())==null){
            return R.fail("变更类型不存在~");
        }
        return snsScoreUserService.changeScore(request);
    }

    /**
     * 分页展示所有用户积分
     * @param pageInfo
     * @param request
     * @return
     */
    @GetMapping("/page")
    public R<Page<SnsScoreUserVO>> snsScoreUserPage(PageInfo pageInfo,SnsScoreUserRequest request){
        return R.data(snsScoreUserService.snsScoreUserPage(pageInfo,request));
    }
}
